using DevExpress.XtraEditors;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using  WinformDevelopFrame.Core.Extend;
using  WinformDevelopFrame.IServices;
using  WinformDevelopFrame.Entity;
using DevExpress.XtraEditors.Repository;
using WinformDevelopFrame.Common;
using WinformDevelopFrame.Core.Form;
namespace  WinformDevelopFrame.UI
{
    public partial class Frmsysdicdata : BaseForm
    {
        #region 自动生成代码
        private ISqlSugarClient _sqlSugarClient;
        private IsysdicdataService _sysdicdataService;
        private ISysDataTableService _sysDataTableService;
        private IsysmenuService _sysmenuService;
        public  Frmsysdicdata(ISqlSugarClient sqlSugarClient, IsysdicdataService sysdicdataService,IsysmenuService sysmenuService,ISysDataTableService sysDataTableService)
        {
            _sqlSugarClient = sqlSugarClient;
            _sysdicdataService = sysdicdataService;
            _sysmenuService = sysmenuService;
            _sysDataTableService = sysDataTableService;
            InitializeComponent();
            Init();
        }

        public override void Form_Load()
        {
            InitListCol();
        }
        /// <summary>
        /// 设置list lookupedit 列
        /// </summary>
        private void InitListCol()
        {
            DicTypeId.Properties.NullText = "";
            gridView1.Columns["DicTypeId"].ColumnEdit = new RepositoryItemLookUpEdit()
            {
                Name = "repositoryItemLookUpEditDicTypeId",
                DataSource = _sqlSugarClient.Ado.GetDataTable(_sysDataTableService
                .QueryByClause(p => p.DataTableName.Equals("字典类型")).SQL),
                DisplayMember = "字典类型",
                ValueMember="Id"
            };
            CreateUserId.Properties.NullText = "";
            gridView1.Columns["CreateUserId"].ColumnEdit = new RepositoryItemLookUpEdit()
            {
                Name = "repositoryItemLookUpEditCreateUserId",
                DataSource = _sqlSugarClient.Ado.GetDataTable(_sysDataTableService
                .QueryByClause(p => p.DataTableName.Equals("用户-lookupedit")).SQL),
                DisplayMember = "用户名称",
                ValueMember="${info.ValueMember}"
            };
            UpdateUserId.Properties.NullText = "";
            gridView1.Columns["UpdateUserId"].ColumnEdit = new RepositoryItemLookUpEdit()
            {
                Name = "repositoryItemLookUpEditUpdateUserId",
                DataSource = _sqlSugarClient.Ado.GetDataTable(_sysDataTableService
                .QueryByClause(p => p.DataTableName.Equals("用户-lookupedit")).SQL),
                DisplayMember = "用户名称",
                ValueMember="${info.ValueMember}"
            };
            
        }
        //设置不可编辑
        private void SetReadOnlyField()
        {
            ReadOnlyList.Add("CreateUserId");
            ReadOnlyList.Add("CreateTime");
            ReadOnlyList.Add("UpdateUserId");
            ReadOnlyList.Add("UpdateTime");
        }
        /// <summary>
        /// 设置数据 设置列名
        /// </summary>
        private void Init()
        {
            fileds.Add("DicTypeId", "字典类型");
            fileds.Add("DicData", "字典数据");
            fileds.Add("Remark", "备注");
            fileds.Add("CreateUserId", "创建人");
            fileds.Add("CreateTime", "创建时间");
            fileds.Add("UpdateUserId", "修改人");
            fileds.Add("UpdateTime", "修改时间");
            SetReadOnlyField();
            var m = _sysmenuService.QueryByClause(p => p.MenuCode.Equals(this.Name));
            //获取工具按钮
            var menus=_sysmenuService.QueryListByClause(p=>p.PId==m.Id).OrderBy(p=>p.OrderNo).ToList();
            menus.ForEach(p =>
            {
                btns.Add(new SimpleButton()
                {
                    Text = p.MenuText,
                    Name = p.MenuCode,
                    Image = Image.FromFile(Application.StartupPath+p.MenuIcon)
                });
            });
            DicTypeId.Properties.DataSource = _sqlSugarClient.Ado.GetDataTable(_sysDataTableService
                .QueryByClause(p => p.DataTableName.Equals("字典类型")).SQL);
            DicTypeId.Properties.DisplayMember = "字典类型";
            DicTypeId.Properties.ValueMember = "Id";
            CreateUserId.Properties.DataSource = _sqlSugarClient.Ado.GetDataTable(_sysDataTableService
                .QueryByClause(p => p.DataTableName.Equals("用户-lookupedit")).SQL);
            CreateUserId.Properties.DisplayMember = "用户名称";
            CreateUserId.Properties.ValueMember = "${info.ValueMember}";
            UpdateUserId.Properties.DataSource = _sqlSugarClient.Ado.GetDataTable(_sysDataTableService
                .QueryByClause(p => p.DataTableName.Equals("用户-lookupedit")).SQL);
            UpdateUserId.Properties.DisplayMember = "用户名称";
            UpdateUserId.Properties.ValueMember = "${info.ValueMember}";
        }
        public override void SetGridData()
        {
            gridControl1.DataSource = _sysdicdataService.Query();
        }
        /// <summary>
        /// 数据校验
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            if (string.IsNullOrEmpty(DicTypeId.Text))
            {
                "字典类型不能为空".ShowTips();
                return false;
            }
            if (string.IsNullOrEmpty(DicData.Text))
            {
                "字典数据不能为空".ShowTips();
                return false;
            }
            return true;
        }
        public override void AddFunction()
        {
            CreateTime.DateTime=DateTime.Now;
            CreateUserId.EditValue = AppInfo.User.Id;
        }
        public override void GridControlDoubleClickFunction()
        {
            ID = ((sysdicdata)gridView1.GetRow(gridView1.FocusedRowHandle)).Id;
        }
        public override void EditFunction()
        {
            UpdateTime.DateTime = DateTime.Now;
            UpdateUserId.EditValue = AppInfo.User.Id;
        }

        public override bool SaveFunction()
        {
            var result = false;
            if (CheckData())
            {
                if(FormStatus==FormStatus.Add)
                {
                    ID=_sysdicdataService.Insert(this.ControlDataToModel<sysdicdata>());
                    result= ID>0;
                }
                else
                {
                    result= _sysdicdataService.Update(this.ControlDataToModel<sysdicdata>());
                }
            }

            return result;
        }

        public override bool DeleteFunction()
        {
            return _sysdicdataService.DeleteById(ID);
        }

        public override bool CancelFunction()
        {
            return base.CancelFunction();
        }
        public override void InitToolBtnStatusFunction(FormStatus status, SimpleButton btn)
        {
            base.InitToolBtnStatusFunction(status,btn);
        }

        public virtual void SetToolBtnFunction()
        {

        }
        #endregion
    }
}